<?php
class model_index extends base_model
{

    public function mod_desktop($args)
    {
        $args['fromTime'] = strtotime($args['fromTime']);
        //$args['fromTime'] = strtotime('-1 week');
        $args['toTime'] = strtotime($args['toTime']) + 86400;
        $redisKey = 'games_count_'.$args['fromTime'].'_'.$args['toTime'].(!empty($args['sKey']) ? '_'.$args['sKey'] : '').'_'.$args['wherefield'];
        $data = $this->redisGet($redisKey);
        if(!$data) {
            $functionRunBeginTimes = microtime(true);
            # 统计的数据如果不是今天的要将数据保存到数据表，以后查询直接取数
            # 统计今天的数据库直接以下过程
            $data['members'] = array(
                0 => 0,
                1 => 0,
                2 => 0,
                3 => 0,
            );
            $sql = "SELECT `usertype`,COUNT(1) AS _TOTAL,IFNULL(SUM(`coin`),0) AS _COIN FROM `@@users` WHERE `regtimes`>=" . $args['fromTime'] . " AND `regtimes`<=" . $args['toTime'] . "  GROUP BY `usertype`";
            //echo $sql;
            $members = $this->db->query($sql, 3);
            if ($members) {
                foreach ($members as $k => $v) {
                    $data['members'][$v['usertype']] = array(
                        'nums' => $v['_TOTAL'],
                        'coin' => $v['_COIN']
                    );
                }
            }
            if(!empty($args['sKey'])){
                # 找出玩家的UID
                $sql = "SELECT `uid` FROM `@@users` WHERE `uno`='".$args['sKey']."' LIMIT 1";
                $uid = $this->db->query($sql, 2);
                $uids = $uid['uid'];
            }else{
                $sql = "SELECT `uid` FROM `@@users` WHERE `usertype`=2 || `usertype`=1";
                $res = $this->db->query($sql, 3);
                $uids = [];
                if( $res ){
                    foreach ( $res as $item ){
                        $uids[] = $item['uid'];
                    }
                }

                $uids = implode(',',$uids);
                $endStr = substr($uids, -1, 1);
                if( $endStr == ',' ){
                    $uids = substr($uids,0,strlen($uids)-1);
                }
            }
            $rechUserType = '';//用户充值类型
            $cashUserType = '';//用户提现类型
            if (!empty($args['wherefield'])) {
                switch ($args['wherefield']) {
                    case "onlymembers" : // 仅会员
                        $rechUserType .= 'AND `usertype`=1';
                        $cashUserType .= 'AND `usertype`=1';
                        break;
                    case "agent"://仅代理
                        $rechUserType .= 'AND `usertype`=2';
                        $cashUserType .= 'AND `usertype`=2';
                        break;
                    default:
                        $rechUserType .= 'AND `usertype`>1 AND `usertype`<3';
                        $cashUserType .= 'AND `usertype`>1 AND `usertype`<3';
                        break;
                }
            }

            # 统计充值
            $sql = 'SELECT IFNULL(SUM(`amount`),0) AS amount,count(`id`) as count,count(distinct uid) as people from `@@users_recharge` where `state`=1 ' . $rechUserType . ' AND `actiontime`>=' . $args['fromTime'] . ' AND `actiontime`<=' . $args['toTime']. ' AND uid IN('.$uids.') AND info<>"注册赠送"';
            $recharge = $this->db->query($sql, 2);
            $data['recharge']['amount'] = $recharge['amount'];
            $data['recharge']['count'] = $recharge['count'];
            $data['recharge']['people'] = $recharge['people'];

            $unionSql = 'SELECT IFNULL(SUM(`amount`),0) AS amount,count(`id`) as count,count(distinct uid) as people from `@@users_recharge` where `state`=1 ' . $rechUserType . ' AND `actiontime`>=' . $args['fromTime'] . ' AND `actiontime`<=' . $args['toTime'] . ' AND paywayid=2 AND uid IN('.$uids.')';
            $unionrecharge = $this->db->query($unionSql, 2);
            $data['recharge']['union_amount'] = $unionrecharge['amount']; //银联转账
            $data['recharge']['union_count'] = $unionrecharge['count'];
            $data['recharge']['union_people'] = $unionrecharge['people'];

            //管理员充值
            $adminSql = 'SELECT IFNULL(SUM(`amount`),0) AS amount,count(`id`) as count,count(distinct uid) 
                        as people from `@@users_recharge` where `state`=1 ' . $rechUserType . ' 
                        AND `actiontime`>=' . $args['fromTime'] . ' AND `actiontime`<=' . $args['toTime'] . ' 
                        AND paywayid=1 AND uid IN('.$uids.') AND info<>"注册赠送"';
            $adminrecharge = $this->db->query($adminSql, 2);
            $data['recharge']['admin_amount'] = $adminrecharge['amount']; //管理员充值
            $data['recharge']['admin_count'] = $adminrecharge['count'];
            $data['recharge']['admin_people'] = $adminrecharge['people'];

            $data['recharge']['other_amount'] = $recharge['amount'] - $unionrecharge['amount'] - $adminrecharge['amount'];//线上支付
            $data['recharge']['other_count'] = $recharge['count'] - $unionrecharge['count'] - $adminrecharge['count'];
            $data['recharge']['other_people'] = $recharge['people'] - $unionrecharge['people'] - $adminrecharge['people'];


            # 统计提现
            $sql = 'SELECT IFNULL(SUM(`amount`),0) AS amount,count(`id`) as count,count(distinct uid) as people from `@@users_cash` where `state`=1 ' . $cashUserType . ' AND `cashtime`>=' . $args['fromTime'] . ' AND `cashtime`<=' . $args['toTime'] . ' AND uid IN('.$uids.')';
            $cash = $this->db->query($sql, 2);
            $data['cash']['amount'] = $cash['amount'];
            $data['cash']['count'] = $cash['count'];
            $data['cash']['people'] = $cash['people'];
            $db_ym = date('Ym');
            $giveliqtype = '23,25,27,30';//23:注册赠送,25:推荐赠送,27:充值赠送,30:代理晋级奖励
            $sql = "SELECT sum(coin) as total,`liqtype` FROM `@@coinlog_".$db_ym."`  WHERE `uid` IN({$uids}) AND `liqtype` IN($giveliqtype) AND `times`>={$args['fromTime']} AND `times`<={$args['toTime']} GROUP BY `liqtype`";
            $give = $this->db->query($sql,3);
            $data['give']['reg_amount'] = 0;
            $data['give']['tj_amount'] = 0;
            $data['give']['rec_amount'] = 0;
            $data['give']['rise_amount'] = 0;
            if( $give ){
                foreach ( $give as $k=>$v ){
                    if( $v['liqtype'] == 23 ){ //注册赠送
                        $data['give']['reg_amount'] = $v['total'];
                    }
                    if( $v['liqtype'] == 25 ){ //推荐赠送
                        $data['give']['tj_amount'] = $v['total'];
                    }
                    if( $v['liqtype'] ==27 ){ //充值赠送
                        $data['give']['rec_amount'] = $v['total'];
                    }
                    if( $v['liqtype'] == 30 ){ //代理晋级奖励
                        $data['give']['rise_amount'] = $v['total'];
                    }
                }
            }
            //玩家盈利

            $data['statistics'] = [];
            $sql = "select gameid,sum(profit) as profit_total,sum(ticket) as ticket_total,sum(winerprofit) as winerprofit_total,sum(betcoin) as betcoin_total from `@@betslog` where uid in({$uids}) and `times`>={$args['fromTime']} AND `times`<={$args['toTime']} group by gameid";
            $statistics = $this->db->query($sql,3);
            $data['statistics']['all']['profit_total'] = 0;
            $data['statistics']['all']['ticket_total'] = 0;
            $data['statistics']['all']['winerprofit_total'] = 0;
            $data['statistics']['all']['betcoin_total'] = 0;

            $data['statistics']['wrdz']['profit'] = 0; //总收益
            $data['statistics']['wrdz']['ticket'] = 0; //总门票
            $data['statistics']['wrdz']['winerprofit'] = 0; //总抽水
            $data['statistics']['wrdz']['betcoin'] = 0; //总参与

            $data['statistics']['nn']['profit'] = 0; //总收益
            $data['statistics']['nn']['ticket'] = 0; //总门票
            $data['statistics']['nn']['winerprofit'] = 0; //总抽水
            $data['statistics']['nn']['betcoin'] = 0; //总参与

            $data['statistics']['sl']['profit'] = 0; //总收益
            $data['statistics']['sl']['ticket'] = 0; //总门票
            $data['statistics']['sl']['winerprofit'] = 0; //总抽水
            $data['statistics']['sl']['betcoin'] = 0; //总参与

            $data['statistics']['ebg']['profit'] = 0; //总收益
            $data['statistics']['ebg']['ticket'] = 0; //总门票
            $data['statistics']['ebg']['winerprofit'] = 0; //总抽水
            $data['statistics']['ebg']['betcoin'] = 0; //总参与

            $data['statistics']['sbn']['profit'] = 0; //总收益
            $data['statistics']['sbn']['ticket'] = 0; //总门票
            $data['statistics']['sbn']['winerprofit'] = 0; //总抽水
            $data['statistics']['sbn']['betcoin'] = 0; //总参与
            foreach ( $statistics as $k=>$v ){
                if( $v['gameid'] == 1 ){
                    $data['statistics']['wrdz']['profit'] = $v['profit_total']; //总收益
                    $data['statistics']['wrdz']['ticket'] = $v['ticket_total']; //总门票
                    $data['statistics']['wrdz']['winerprofit'] = $v['winerprofit_total']; //总抽水
                    $data['statistics']['wrdz']['betcoin'] = $v['betcoin_total']; //总参与
                }
                if( $v['gameid'] == 2 ){
                    $data['statistics']['nn']['profit'] = $v['profit_total']; //总收益
                    $data['statistics']['nn']['ticket'] = $v['ticket_total']; //总门票
                    $data['statistics']['nn']['winerprofit'] = $v['winerprofit_total']; //总抽水
                    $data['statistics']['nn']['betcoin'] = $v['betcoin_total']; //总参与
                }
                if( $v['gameid'] == 3 ){
                    $data['statistics']['sl']['profit'] = $v['profit_total']; //总收益
                    $data['statistics']['sl']['ticket'] = $v['ticket_total']; //总门票
                    $data['statistics']['sl']['winerprofit'] = $v['winerprofit_total']; //总抽水
                    $data['statistics']['sl']['betcoin'] = $v['betcoin_total']; //总参与
                }
                if( $v['gameid'] == 4 ){
                    $data['statistics']['ebg']['profit'] = $v['profit_total']; //总收益
                    $data['statistics']['ebg']['ticket'] = $v['ticket_total']; //总门票
                    $data['statistics']['ebg']['winerprofit'] = $v['winerprofit_total']; //总抽水
                    $data['statistics']['ebg']['betcoin'] = $v['betcoin_total']; //总参与
                }
                if( $v['gameid'] == 5 ){
                    $data['statistics']['sbn']['profit'] = $v['profit_total']; //总收益
                    $data['statistics']['sbn']['ticket'] = $v['ticket_total']; //总门票
                    $data['statistics']['sbn']['winerprofit'] = $v['winerprofit_total']; //总抽水
                    $data['statistics']['sbn']['betcoin'] = $v['betcoin_total']; //总参与
                }
                $data['statistics']['all']['profit_total'] += $v['profit_total']; //总收益
                $data['statistics']['all']['ticket_total'] += $v['ticket_total']; //总门票
                $data['statistics']['all']['winerprofit_total'] += $v['winerprofit_total']; //总抽水
                $data['statistics']['all']['betcoin_total'] += $v['betcoin_total']; //总参与
            }
            $functionRunEndTimes = microtime(true);
            $data['countdate'] = date('Y-m-d H:i:s', time());
            $data['consuming'] = (round(($functionRunEndTimes - $functionRunBeginTimes), 3) * 1000);
            if($args['fromTime'] == strtotime(date('Y-m-d',time()))){
                $expire = 120;
            }else{
                $expire = 86400 * 31;
            }
            $this->redisSetEx($redisKey, $data, $expire);
        }
        return $data;
    }
}